/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Payment;

/**
 *
 * @author Danh
 */
public class PaymenManager {

    Connection conn;

    public PaymenManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Payment> getPayment() {
        List<Payment> list = new ArrayList<Payment>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Payment_Types");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Payment c = new Payment(rs.getInt("Payment_Type_Id"), rs.getBoolean("Payment_Type_Status"), rs.getNString("Payment_Type_Name"));
                list.add(c);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insertPayment(Payment c) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Payment_Types(Payment_Type_Status,Payment_Type_Name) VALUES(?,?)");
            pstmt.setBoolean(1, c.isStatus());
            pstmt.setNString(2, c.getName());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public boolean update(Payment c) {
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement("UPDATE Payment_Types SET Payment_Type_Status=? ,Payment_Type_Name =? WHERE Payment_Type_Id=?");
            pstmt.setBoolean(1, c.isStatus());
            pstmt.setNString(2, c.getName());
            pstmt.setInt(3, c.getId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deletePayment(int id) {
        try {
            PreparedStatement pstm = conn.prepareStatement("DELETE FROM Payment_Details WHERE Payment_Type_Id=?");
            pstm.setInt(1, id);
            pstm.executeUpdate();
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Payment_Types  WHERE Payment_Type_Id=?");
            pstmt.setInt(1, id);
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }
}
